﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace CH.Easy.OpenLogin
{
    public class QZoneOpenAPI:AbstractOpenAPI
    {
        public QZoneOpenAPI(AbstractOauthV2 oAuth)
            : base(oAuth)
        {

        }


        public override string GetAuthorizeUrl()
        {
            return base.oAuth.GetAuthorizeUrl();
        }

        public override AccessTokenDTO GetAccessToken(string code)
        {
            return base.oAuth.GetAccessToken(code);
        }

        public override CallBackDTO GetOpenID(string accessToken)
        {
            return base.oAuth.GetOpenID(accessToken);
        }


        public override UserDTO GetUserInfo(string format)
        {
            Dictionary<string, string> param = new Dictionary<string, string>()
            {
                {"access_token",base.oAuth.AccessToken},
                {"oauth_consumer_key",base.oAuth.AppKey},
                {"openid",base.oAuth.OpenID},
                {"format","json"}
            };
            string url = "https://graph.qq.com/user/get_info";
            string json = base.oAuth.OpenRequest.HttpGet(url, param.GetQueryString());
            UserDTO userDTO = new UserDTO();
            userDTO.json = json;
            var qZone = new
            {
                seqid = "",
                ret = "",
                data = new
                {
                    birth_day = "",
                    birth_month = "",
                    birth_year = "",
                    name = "",
                    nick = "",
                    openid = "",
                    sex = "",
                    location = "",
                    head = ""
                }
            };

            
            
            var qZoneResult = JsonConvert.DeserializeAnonymousType(json, qZone);
            string[] addressArry = null;
            try
            {
                addressArry = qZoneResult.data.location.Split(' ');
                userDTO.province = addressArry[1];
                userDTO.city = addressArry[2];

            }
            catch (Exception)
            {
                userDTO.province = "";
                userDTO.city = "";
                
            }



            userDTO.birth_day = qZoneResult.data.birth_day;
            userDTO.birth_month = qZoneResult.data.birth_month;
            userDTO.birth_year = qZoneResult.data.birth_year;
            //100px，50px，30px
            userDTO.head_big = qZoneResult.data.head + "/100";
            userDTO.head_medium = qZoneResult.data.head + "/50";
            userDTO.head_small = qZoneResult.data.head + "/30";
            userDTO.name = qZoneResult.data.name;
            userDTO.nick = qZoneResult.data.nick;
            userDTO.openid = qZoneResult.data.openid;
            //1：男； 2：女； 0：未填写。 
            //这里统一性别
            if ("2".Equals(qZoneResult.data.sex))
            {
                userDTO.sex = "0";//小美女
            }
            else if ("1".Equals(qZoneResult.data.sex))
            {
                userDTO.sex = "1";//小帅哥
            }
            else
            {
                userDTO.sex = "2";//保密
            }
            userDTO.is_succeed = true;
            userDTO.open_type = base.oAuth.OpenType;

            return userDTO;
           
        }

        public override ResultDTO AddWeibo(string format, string content, string clientip, string jing, string wei, string syncflag)
        {
            //Dictionary<string, string> param = new Dictionary<string, string>()
            //{
            //    {"access_token",base.oAuth.AccessToken},
            //    {"oauth_consumer_key",base.oAuth.AppKey},
            //    {"openid",base.oAuth.OpenID},
            //    {"format",format},
            //    {"con",content},
            //    {"third_source","1"}
            //};
            //string url = "https://graph.qq.com/shuoshuo/add_topic";
            //return base.oAuth.OpenRequest.HttpPost(url, param.GetQueryString()).JsonToObject(base.oAuth.OpenType);



            Dictionary<string, string> param = new Dictionary<string, string>()
            {
                {"access_token",base.oAuth.AccessToken},
                {"oauth_consumer_key",base.oAuth.AppKey},
                {"openid",base.oAuth.OpenID},
                {"format",format},
                {"content",content},
                {"clientip",clientip},
                {"jing",jing},
                {"wei",wei},
                {"syncflag",syncflag}
            };
            string url = "https://graph.qq.com/t/add_t";
            return base.oAuth.OpenRequest.HttpPost(url, param.GetQueryString()).JsonToObject(base.oAuth.OpenType);
        }

        public override ResultDTO AddPicWeiBo(string format, string content, string clientip, string jing, string wei, string picUrl, string syncflag)
        {
            //Dictionary<string, string> param = new Dictionary<string, string>()
            //{
            //    {"access_token",base.oAuth.AccessToken},
            //    {"oauth_consumer_key",base.oAuth.AppKey},
            //    {"openid",base.oAuth.OpenID},
            //    {"format",format},
            //    {"richtype","1"},
            //    {"url",picUrl},
            //    {"height","700"},
            //    {"weight","700"},
            //    {"con",content},
            //    {"third_source","1"}
            //};

            //string url = "https://graph.qq.com/shuoshuo/add_topic";
            //return base.oAuth.OpenRequest.HttpPost(url, param.GetQueryString()).JsonToObject(base.oAuth.OpenType);

            Dictionary<string, string> param = new Dictionary<string, string>()
            {
                {"access_token",base.oAuth.AccessToken},
                {"oauth_consumer_key",base.oAuth.AppKey},
                {"openid",base.oAuth.OpenID},
                {"format",format},
                {"content",content},
                {"clientip",clientip},
                {"jing",jing},
                {"wei",wei},
                {"syncflag",syncflag}
            };
            
            string url = "https://graph.qq.com/t/add_pic_t";
            return base.oAuth.OpenRequest.HttpPostWithImage(url, param, picUrl).JsonToObject(base.oAuth.OpenType);
        }



        
    }
}
